function [output]=sort_act(ROI_act,runM,runP,stim_id,shutterOpen,order)
feedback=[];
playback=[];
baseline=[];
fb_cnt=1;
pb_cnt=1;
bs_cnt=1;
for ind=1:length(order)
    if order(ind)==1
        cur_ROI_act=ROI_act(shutterOpen((ind*2)-1):shutterOpen(ind*2));
        cur_runM=runM(shutterOpen((ind*2)-1):shutterOpen(ind*2));
        cur_stim_id=stim_id(shutterOpen((ind*2)-1):shutterOpen(ind*2));
        %                     .baseline
        feedback(1,fb_cnt)=mean(cur_ROI_act(cur_runM==0));
        %                     .feedback
        feedback(2,fb_cnt)=mean(cur_ROI_act(cur_runM==1&cur_stim_id==0));
        %                     .FFpert
        feedback(3,fb_cnt)=mean(cur_ROI_act(cur_runM==1&cur_stim_id==7));
        %                     .LFMpert_neg6
        feedback(4,fb_cnt)=mean(cur_ROI_act(cur_runM==1&cur_stim_id==-6));
        %                     .LFMpert_neg5
        feedback(5,fb_cnt)=mean(cur_ROI_act(cur_runM==1&cur_stim_id==-5));
        %                     .LFMpert_neg4
        feedback(6,fb_cnt)=mean(cur_ROI_act(cur_runM==1&cur_stim_id==-4));
        %                     .LFMpert_neg3
        feedback(7,fb_cnt)=mean(cur_ROI_act(cur_runM==1&cur_stim_id==-3));
        %                     .LFMpert_neg2
        feedback(8,fb_cnt)=mean(cur_ROI_act(cur_runM==1&cur_stim_id==-2));
        %                     .LFMpert_neg1
        feedback(9,fb_cnt)=mean(cur_ROI_act(cur_runM==1&cur_stim_id==-1));
        %                     .LFMpert1
        feedback(10,fb_cnt)=mean(cur_ROI_act(cur_runM==1&cur_stim_id==1));
        %                     .LFMpert2
        feedback(11,fb_cnt)=mean(cur_ROI_act(cur_runM==1&cur_stim_id==2));
        %                     .LFMpert3
        feedback(12,fb_cnt)=mean(cur_ROI_act(cur_runM==1&cur_stim_id==3));
        %                     .LFMpert4
        feedback(13,fb_cnt)=mean(cur_ROI_act(cur_runM==1&cur_stim_id==4));
        %                     .LFMpert5
        feedback(14,fb_cnt)=mean(cur_ROI_act(cur_runM==1&cur_stim_id==5));
        %                     .LFMpert6
        feedback(15,fb_cnt)=mean(cur_ROI_act(cur_runM==1&cur_stim_id==6));
        fb_cnt=fb_cnt+1;
    elseif order(ind)==2
        cur_ROI_act=ROI_act(shutterOpen((ind*2)-1):shutterOpen(ind*2));
        cur_runM=runM(shutterOpen((ind*2)-1):shutterOpen(ind*2));
        cur_stim_id=stim_id(shutterOpen((ind*2)-1):shutterOpen(ind*2));
        cur_runP=runP(shutterOpen((ind*2)-1):shutterOpen(ind*2));
        %                     .baseline
        playback(1,pb_cnt)=mean(cur_ROI_act(cur_runM==0&cur_runP==0));
        %                     .playback
        playback(2,pb_cnt)=mean(cur_ROI_act(cur_runM==0&cur_runP==1&cur_stim_id==0));
        %                     .feedback
        playback(3,pb_cnt)=mean(cur_ROI_act(cur_runM==1&cur_runP==1&cur_stim_id==0));
        %                     .FFMdecoupled
        playback(4,pb_cnt)=mean(cur_ROI_act(cur_runM==1&cur_runP==0));
        %                     .FFMpert_fb
        playback(5,pb_cnt)=mean(cur_ROI_act(cur_runM==1&cur_runP==1&cur_stim_id==7));
        %                     .LFMpert_neg6_fb
        playback(6,pb_cnt)=mean(cur_ROI_act(cur_runM==1&cur_runP==1&cur_stim_id==-6));
        %                     .LFMpert_neg5_fb
        playback(7,pb_cnt)=mean(cur_ROI_act(cur_runM==1&cur_runP==1&cur_stim_id==-5));
        %                     .LFMpert_neg4_fb
        playback(8,pb_cnt)=mean(cur_ROI_act(cur_runM==1&cur_runP==1&cur_stim_id==-4));
        %                     .LFMpert_neg3_fb
        playback(9,pb_cnt)=mean(cur_ROI_act(cur_runM==1&cur_runP==1&cur_stim_id==-3));
        %                     .LFMpert_neg2_fb
        playback(10,pb_cnt)=mean(cur_ROI_act(cur_runM==1&cur_runP==1&cur_stim_id==-2));
        %                     .LFMpert_neg1_fb
        playback(11,pb_cnt)=mean(cur_ROI_act(cur_runM==1&cur_runP==1&cur_stim_id==-1));
        %                     .LFMpert1_fb
        playback(12,pb_cnt)=mean(cur_ROI_act(cur_runM==1&cur_runP==1&cur_stim_id==1));
        %                     .LFMpert2_fb
        playback(13,pb_cnt)=mean(cur_ROI_act(cur_runM==1&cur_runP==1&cur_stim_id==2));
        %                     .LFMpert3_fb
        playback(14,pb_cnt)=mean(cur_ROI_act(cur_runM==1&cur_runP==1&cur_stim_id==3));
        %                     .LFMpert4_fb
        playback(15,pb_cnt)=mean(cur_ROI_act(cur_runM==1&cur_runP==1&cur_stim_id==4));
        %                     .LFMpert5_fb
        playback(16,pb_cnt)=mean(cur_ROI_act(cur_runM==1&cur_runP==1&cur_stim_id==5));
        %                     .LFMpert6_fb
        playback(17,pb_cnt)=mean(cur_ROI_act(cur_runM==1&cur_runP==1&cur_stim_id==6));
        %                     .FFMpert_pb
        playback(18,pb_cnt)=mean(cur_ROI_act(cur_runM==0&cur_runP==1&cur_stim_id==7));
        %                     .LFMpert_neg6_pb
        playback(19,pb_cnt)=mean(cur_ROI_act(cur_runM==0&cur_runP==1&cur_stim_id==-6));
        %                     .LFMpert_neg5_pb
        playback(20,pb_cnt)=mean(cur_ROI_act(cur_runM==0&cur_runP==1&cur_stim_id==-5));
        %                     .LFMpert_neg4_pb
        playback(21,pb_cnt)=mean(cur_ROI_act(cur_runM==0&cur_runP==1&cur_stim_id==-4));
        %                     .LFMpert_neg3_pb
        playback(22,pb_cnt)=mean(cur_ROI_act(cur_runM==0&cur_runP==1&cur_stim_id==-3));
        %                     .LFMpert_neg2_pb
        playback(23,pb_cnt)=mean(cur_ROI_act(cur_runM==0&cur_runP==1&cur_stim_id==-2));
        %                     .LFMpert_neg1_pb
        playback(24,pb_cnt)=mean(cur_ROI_act(cur_runM==0&cur_runP==1&cur_stim_id==-1));
        %                     .LFMpert1_pb
        playback(25,pb_cnt)=mean(cur_ROI_act(cur_runM==0&cur_runP==1&cur_stim_id==1));
        %                     .LFMpert2_pb
        playback(26,pb_cnt)=mean(cur_ROI_act(cur_runM==0&cur_runP==1&cur_stim_id==2));
        %                     .LFMpert3_pb
        playback(27,pb_cnt)=mean(cur_ROI_act(cur_runM==0&cur_runP==1&cur_stim_id==3));
        %                     .LFMpert4_pb
        playback(28,pb_cnt)=mean(cur_ROI_act(cur_runM==0&cur_runP==1&cur_stim_id==4));
        %                     .LFMpert5_pb
        playback(29,pb_cnt)=mean(cur_ROI_act(cur_runM==0&cur_runP==1&cur_stim_id==5));
        %                     .LFMpert6_pb
        playback(30,pb_cnt)=mean(cur_ROI_act(cur_runM==0&cur_runP==1&cur_stim_id==6));
        pb_cnt=pb_cnt+1;
    else
        cur_ROI_act=ROI_act(shutterOpen((ind*2)-1):shutterOpen(ind*2));
        cur_runM=runM(shutterOpen((ind*2)-1):shutterOpen(ind*2));
%         .run
        baseline(1,bs_cnt)=mean(cur_ROI_act(cur_runM==1));
%         .sit
        baseline(2,bs_cnt)=mean(cur_ROI_act(cur_runM==0));
        bs_cnt=bs_cnt+1;
    end
end

feedback(isnan(feedback))=0;
playback(isnan(playback))=0;
baseline(isnan(baseline))=0;

fb_av=mean(feedback,2);
pb_av=mean(playback,2);
b_av=mean(baseline,2);

output=[fb_av' pb_av' b_av'];